package cn.pconline.autoclub.web.admin;

import cn.pconline.autoclub.entity.Club;
import cn.pconline.autoclub.entity.Topic;
import cn.pconline.autoclub.service.ClubService;
import cn.pconline.autoclub.service.MemberService;
import cn.pconline.autoclub.service.PagerService;
import cn.pconline.autoclub.util.Pager;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.lang.StringUtils;
import org.gelivable.dao.GeliDao;
import org.gelivable.dao.GeliOrm;
import org.gelivable.dao.SqlBuilder;
import org.gelivable.web.Env;
import org.gelivable.web.EnvUtils;
import org.gelivable.webmvc.JSONBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;


/**
 * @author zhouruxuan
 * @date 2014年9月17日 上午11:34:21
 */
@Controller
@RequestMapping("/admin")
public class SyncPickTopic4ClubAdminController {
	
	private static final Logger log = LoggerFactory.getLogger(SyncPickTopic4ClubAdminController.class);
	
	@Autowired
	private ClubService clubService;
	@Autowired
	private MemberService memberService;
	@Autowired
	private GeliOrm geliOrm;
	@Autowired
	PagerService pagerService;
	@Autowired
	GeliDao geliDao;
	
	/**
	 * 所有车友会会长列表
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/dev/listClubAdmin.do")
	public String listClubAdmin(HttpServletRequest request, HttpServletResponse response) {
		Env env = EnvUtils.getEnv();
		
		String name = env.param("name", "").trim();
        String begin = env.param("beginDate", "");
        String end = env.param("endDate", "");
		int pageNum = env.paramInt("pageNum", 1);
        int pageSize = env.paramInt("pageSize", 20);
        //pageSize=100;
		
		SqlBuilder sqlBuilder = new SqlBuilder();
		sqlBuilder.appendSql("select * from ").appendSql(geliOrm.getTableName(Club.class));
		sqlBuilder.appendSql(" where 1=1 ");
		if(StringUtils.isEmpty(name) == false) {
			sqlBuilder.appendSql(" and name like ").appendValue("%" + name + "%");
		}
		if (StringUtils.isNotBlank(begin)) {
			sqlBuilder.appendSql(" and create_at >=").appendValue(begin);
        }
        if (StringUtils.isNotBlank(end)) {
        	sqlBuilder.appendSql(" and create_at <=").appendValue(end);
        }
		
		Pager<Club> pager = pagerService.pager(Club.class, sqlBuilder, pageNum, pageSize);
        

		request.setAttribute("pager", pager);
		request.setAttribute("statusDesc", Club.statusDesc);
		request.setAttribute("name", name);
		request.setAttribute("beginDate", begin);
		request.setAttribute("endDate", end);
		
		return "admin/dev/listClubAdmin";
	}

	/**
	 * 同步所有车友会创建人的精华帖子到车友会
	 * 不管车友会状态
	 * @param request
	 * @param response
	 * @return
	 * @throws IOException 
	 */
	@RequestMapping(value = "/dev/syncPickTopic4ClubAdmin.do")
	public String syncPickTopic4ClubAdmin(HttpServletRequest request, HttpServletResponse response) throws IOException {
		String[] ids = request.getParameterValues("ids[]");
		String pickType = request.getParameter("pickType");
		
		List<Topic> topicList = new ArrayList<Topic>();//返回同步的精华帖子列表
		List<Topic> topics;//返回所有同步操作的精华帖子列表
		if("select".equals(pickType)) {//获取选择中的会长
			for (String clubId : ids) {
				Club club = clubService.getClub(Long.parseLong(clubId));
				if(club != null) {
					topics = memberService.loadPickByUserIdClubId(club.getClubId(), club.getCreateBy());
					topicList.addAll(topics);
				}
			}
		} else if("all".equals(pickType)) {//获取所有会长
			List<Club> clubList = clubService.listAllClub();
			for(Club club2 : clubList) {
				topics = memberService.loadPickByUserIdClubId(club2.getClubId(), club2.getCreateBy());
				topicList.addAll(topics);
			}
		} else {
			response.setCharacterEncoding("UTF-8");
			response.getWriter().println(
					new JSONBuilder().put("statusCode", 300).put("message", "非法参数pickType!").toString());
		}
		
		log.warn("会长精华帖子同步开始......................................");
		for(Topic topic : topicList) {
			log.warn("topicId=" + topic.getTopicId() + ", clubId=" + topic.getClubId() + ",topicTitle=" + topic.getTopicTitle());
		}
		log.warn("会长精华帖子同步结束......................................");
		log.warn("同步精华帖子数量：" + topicList.size());
		
		response.setCharacterEncoding("UTF-8");
		response.getWriter().println(
				new JSONBuilder().put("statusCode", 200).put("message", "操作成功<br>同步精华帖子个数："+topicList.size()).toString());
				
		return null;
	}
}
